package net.betou.cms.dao.main.impl;

import java.util.List;

import net.betou.cms.dao.main.CmsModelItemDao;
import net.betou.cms.entity.main.CmsModelItem;
import net.betou.common.hibernate3.Finder;
import net.betou.common.hibernate3.HibernateBaseDao;

import org.springframework.stereotype.Repository;

@Repository
public class CmsModelItemDaoImpl extends HibernateBaseDao< CmsModelItem, Integer > implements CmsModelItemDao{
	@SuppressWarnings( "unchecked" )
	public List< CmsModelItem > getList( Integer modelId, boolean isChannel, boolean hasDisabled ) {
		StringBuilder sb = new StringBuilder( "from CmsModelItem bean where bean.model.id=? and bean.channel=?" );
		if( !hasDisabled ){
			sb.append( " and bean.display=true" );
		}
		sb.append( " order by bean.priority asc,bean.id asc" );
		return find( sb.toString( ), modelId, isChannel );
	}
	
	@SuppressWarnings( "unchecked" )
	public List< CmsModelItem > getList( Integer modelId, boolean isChannel, boolean hasDisabled, String searchValue, String searchProperty, String orderProperty, String orderDirection ) {
		
		Finder f = Finder.create( "from CmsModelItem bean where bean.model.id=:modelId and bean.channel=:isChannel" );
		f.setParam( "modelId", modelId );
		f.setParam( "isChannel", isChannel );
		
		if( !hasDisabled ){
			f.append( "and bean.display=:display" );
			f.setParam( "display", true );
		}
		
		createSearch( f, searchProperty, searchValue );
		createOrder( f, orderProperty, orderDirection, "priority" );
		
		return find( f );
		//return find( sb.toString( ), modelId, isChannel );
	}
	
	

	public CmsModelItem findById( Integer id ) {
		CmsModelItem entity = get( id );
		return entity;
	}

	public CmsModelItem save( CmsModelItem bean ) {
		getSession( ).save( bean );
		return bean;
	}

	public CmsModelItem deleteById( Integer id ) {
		CmsModelItem entity = super.get( id );
		if( entity != null ){
			getSession( ).delete( entity );
		}
		return entity;
	}

	@Override
	protected Class< CmsModelItem > getEntityClass( ) {
		return CmsModelItem.class;
	}
}